<template>
  <div class="app-container">
    <div v-if="crud.props.searchToggle">
      <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <rrOperation/>
    </div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="right">
          <el-button
            class="filter-item"
            size="mini"
            type="success"
            @click="submit(crud.selections[0])"
            :disabled="crud.selections.length !== 1"
          >
            上传图片
          </el-button>
        </template>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!--          <el-form-item label="科室" prop="administrativeId">-->
          <!--            <el-select v-model="form.administrativeId" placeholder="请选择">-->
          <!--              <el-option-->
          <!--                v-for="item in options"-->
          <!--                :key="item.id"-->
          <!--                :label="item.mnName"-->
          <!--                :value="item.id">-->
          <!--              </el-option>-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item label="出生日期" prop="birthday">
            <el-date-picker v-model="form.birthday" type="datetime" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="从医时间" prop="medicalTime">
            <el-date-picker v-model="form.medicalTime" type="datetime" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <template>
              <el-radio v-model="form.sex" label="0">男</el-radio>
              <el-radio v-model="form.sex" label="1">女</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="毕业学校" prop="school">
            <el-input v-model="form.school" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="学历" prop="educationalBackground">
            <el-select v-model="form.educationalBackground" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.educational_background"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="职称" prop="nickname">
            <el-select v-model="form.nickname" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.professional_post"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="专家" prop="expert">
            <template>
              <el-radio v-model="form.expert" label="1">是</el-radio>
              <el-radio v-model="form.expert" label="0">否</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="姓名(蒙)" prop="mnName">
            <el-input v-model="form.mnName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="姓名(汉)" prop="zhName">
            <el-input v-model="form.zhName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="介绍(蒙)" >
            <el-input v-model="form.mnIntroduce" :rows="3" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="介绍(汉)" p>
            <el-input v-model="form.zhIntroduce" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="administrativeName" label="科室"/>
        <el-table-column prop="birthday" label="出生日期"/>
        <el-table-column prop="medicalTime" label="从医时间"/>

        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span v-if="scope.row.sex === '1'">女</span>
            <span v-else>男</span>
          </template>
        </el-table-column>

        <el-table-column prop="school" label="毕业学校"/>
        <el-table-column prop="educationalBackground" label="学历">
          <template slot-scope="scope">
            {{ dict.label.educational_background[scope.row.educationalBackground] }}
          </template>
        </el-table-column>
        <el-table-column prop="nickname" label="职称">
          <template slot-scope="scope">
            {{ dict.label.professional_post[scope.row.nickname] }}
          </template>
        </el-table-column>
        <el-table-column prop="expert" label="专家">
          <template slot-scope="scope">
            <span v-if="scope.row.expert === '1'">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column prop="mnName" label="姓名(蒙)"/>
        <el-table-column prop="zhName" label="姓名(汉)"/>
        <el-table-column prop="zhIntroduce" label="介绍(汉)"/>

        <el-table-column prop="image" label="照片" align="center">
          <template slot-scope="scope">
            <el-image
              :src="baseApi + '/file' +scope.row.image"
              :preview-src-list="[baseApi + '/file' + scope.row.image]"
              fit="contain"
              lazy
              class="el-avatar"
            >
              <div slot="error">
                <i class="el-icon-document"/>
              </div>
            </el-image>
          </template>

        </el-table-column>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateBy" label="修改者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="150px" align="center"
                         v-if="checkPer(['admin','administrativeOffice:edit','administrativeOffice:del'])">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <!--      <pagination/>-->
      <pagination/>
      <el-dialog
        title="提示"
        :visible.sync="dialog"
        width="30%">
        <el-upload
          class="upload-demo"
          drag
          :headers="headers"
          :action="action"
          :on-success="handleAvatarSuccess"
          :show-file-list="true"
          multiple>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
        <span slot="footer" class="dialog-footer">
           <el-button @click="dialog = false">取 消</el-button>
       <el-button type="primary" @click="dialog = false">确 定</el-button>
      </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import crudDoctor from '@/api/medicine/doctor'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getToken} from "@/utils/auth";
import {getByName} from "@/api/medicine/doctor";
import {getDoctorByOfficeId} from "@/api/medicine/doctor";
import {updateDoctor, addDoctor, delDoctor} from "@/api/medicine/doctor";

const defaultForm = {
  id: null,
  administrativeId: null,
  birthday: null,
  medicalTime: null,
  sex: null,
  school: null,
  educationalBackground: null,
  nickname: null,
  expert: null,
  mnName: null,
  mnNameCode: null,
  zhName: null,
  mnIntroduce: null,
  mnIntroduceCode: null,
  zhIntroduce: null,
  pathImage: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  remark: null,
  status: null
}
export default {
  name: 'Doctor',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['educational_background', 'professional_post'],
  cruds() {
    return CRUD({title: '医生管理', url: 'api/doctor', idField: 'id', sort: 'id,desc', crudMethod: {...crudDoctor}})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'doctor:add'],
        edit: ['admin', 'doctor:edit'],
        del: ['admin', 'doctor:del']
      },
      ids: [],
      selectedRows: '',
      options: [],
      value: '',
      input: '',
      rules: {
        administrativeId: [
          {required: true, message: '请选择科室', trigger: 'change'}
        ],
        birthday: [
          {required: true, message: '请选择出生年日期', trigger: 'change'}
        ],
        medicalTime: [
          {required: true, message: '请选择从医时间', trigger: 'change'}
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ],
        shool: [
          {required: true, message: '请输入毕业学校', trigger: 'change'}
        ],
        educationalBackground: [
          {required: true, message: '请选择学历', trigger: 'change'}
        ],
        nickname: [
          {required: true, message: '请选择职称', trigger: 'change'}
        ],
        expert: [
          {required: true, message: '请选择是否为专家', trigger: 'change'}
        ],
        mnName: [
          {required: true, message: '请输入名称(蒙)', trigger: 'change'}
        ],
        zhName: [
          {required: true, message: '请输入名称(汉)', trigger: 'change'}
        ],
        mnIntroduce: [
          {required: true, message: '请输入介绍(蒙)', trigger: 'change'}
        ],
        zhIntroduce: [
          {required: true, message: '请输入介绍(汉)', trigger: 'change'}
        ]
      },
      action: null,
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      dialog: false,
      dialogVisible: false,
      visible: false
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params.administrativeId = this.$route.query.param
      return true
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudDoctor.edit(data).then(res => {
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        if (val === 1) {
          data.status = 0;
        } else {
          data.status = 1;
        }
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },

    // 当分页大小改变时触发
    submit(row) {
      this.dialog = true;
      this.action = "/api/doctor/uploadProcessDefinition/" + row.id
    },
    handleAvatarSuccess() {
      this.$message('上传成功！');
    },
    search() {
      getByName(encodeURIComponent(this.input)).then(res => {
        this.crud.data = res;
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {

        });
    }
  },
  mounted() {
    defaultForm.administrativeId = this.$route.query.param;
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'fileUploadApi',
      'imagesUploadApi'
    ])
  }
}
</script>

<style scoped>

</style>
